Sorter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 21
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A order 0 3 1
A create 0 5 1
A constructor 0 4 2
A compare 0 22 5
1
class Sorter {
2
    constructor(key, direction) {
3 4
        this.key = key;
4 4
        this.moveDirection = direction === 'desc' ? -1 : 1;
5
    }
6
7
    order(original) {
8 4
        return original.sort(this.compare.bind(this));
9
    }
10
11
    compare(firstElement, secondElement) {
12 22
        let nameA = firstElement[this.key];
13 22
        let nameB = secondElement[this.key];
14
15 22
        if (typeof nameA === 'string') {
16 5
            nameA = nameA.toUpperCase();
17
        }
18
19 22
        if (typeof nameB === 'string') {
20 5
            nameB = nameB.toUpperCase();
21
        }
22
23 22
        if (nameA < nameB) {
24 8
            return -1 * this.moveDirection;
25
        }
26
27 14
        if (nameA > nameB) {
28 10
            return 1 * this.moveDirection;
29
        }
30
31 4
        return 0;
32
    }
33
34
    static create(original, key, direction) {
35 4
        const sorter = new Sorter(key, direction);
36
37 4
        return sorter.order(original);
38
    }
39
}
40
41
export default function multisort(original, key, direction) {
42 4
    return Sorter.create(original, key, direction);
43
}
44